package com.tjsinfo.tjsoa.system.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemYiZhangBO;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemYinzhang;

public class InitSelectSealAction extends Action {
	private ISystemUserBo systemUserBo;
	private ISystemUserDepartmentBo systemUserDepartmentBo;
	private IDepartmentBo departmentBo;
	private ISystemYiZhangBO systemYiZhangBO;
	
	
	
	public ISystemYiZhangBO getSystemYiZhangBO() {
		return systemYiZhangBO;
	}

	public void setSystemYiZhangBO(ISystemYiZhangBO systemYiZhangBO) {
		this.systemYiZhangBO = systemYiZhangBO;
	}

	public IDepartmentBo getDepartmentBo() {
		return departmentBo;
	}

	public void setDepartmentBo(IDepartmentBo departmentBo) {
		this.departmentBo = departmentBo;
	}

	public ISystemUserDepartmentBo getSystemUserDepartmentBo() {
		return systemUserDepartmentBo;
	}

	public void setSystemUserDepartmentBo(
			ISystemUserDepartmentBo systemUserDepartmentBo) {
		this.systemUserDepartmentBo = systemUserDepartmentBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try{
			JSONArray array = new JSONArray();		
			List<TSystemUserDepartment> systemuserdepartmentlist =  systemUserDepartmentBo.selectByUserId(Integer.parseInt(request.getParameter("userId")) );
			List<String> departmentlist = new ArrayList<String>();
			for (TSystemUserDepartment systemUserDepartment : systemuserdepartmentlist) {
				departmentlist.add(systemUserDepartment.getDepartmentId().trim());
			}
			List<TSystemYinzhang> systemYinzhangList = new ArrayList<TSystemYinzhang>(); 
			for (String departmentId : departmentlist) {	 
				 System.out.println(departmentId+"aa");
				List<TSystemYinzhang> tempList =  systemYiZhangBO.selectYinZhangByDepartmentId(departmentId.trim());
				 systemYinzhangList.addAll(tempList);
				
			}
			System.out.println("size" + systemYinzhangList.size());
			
			for (TSystemYinzhang systemYinzhang : systemYinzhangList) {
				JSONObject object = new JSONObject();
				object.put("name", systemYinzhang.getTSystemYinzhangjiben().getYinzhangName());
				object.put("value", systemYinzhang.getTSystemYinzhangjiben().getYinzhangValue());
				array.put(object);
			}
			response.getWriter().print(array);
			

		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
}

